import { moment } from 'moment';
/*
 * umi配置
 */

import { defineConfig } from 'umi';
// import SentryPlugin from '@sentry/webpack-plugin';
import pluginConfig from './plugin.config';
// import babelConfig from './babel.config';
import proxyConfig from './proxy.config';
import themeConfig from './theme.config';
import pageRoutes from './router';
const FileManagerPlugin = require('filemanager-webpack-plugin');


const { UMI_ENV, ROLE, npm_package_version, VCONSOLE } = process.env;

console.info('当前app环境---->', UMI_ENV);
console.info('当前role---->', ROLE);
console.info('当前VCONSOLE---->', VCONSOLE)

const headScripts = VCONSOLE === 'true'
  ? ['https://cdn.staticfile.org/vConsole/3.3.4/vconsole.min.js']
  : [];

export default defineConfig({
  plugins: pluginConfig,
  request: {
    dataField: '',
  },
  hd: {
    theme: themeConfig,
    px2rem: {
      rootValue: 100,
      propBlackList: [
        'border-top-width',
        'border-left-width',
        'border-right-width',
        'border-bottom-width',
      ], // 这些属性不需要转换
      selectorBlackList: ['no_hd'], // 以包含no_hd的class不需要转换
      // exclude: /(node_module)/
    },
  },
  dva: {
    immer: true,
  },
  antd: {},
  dynamicImport: {},
  title: '建筑师训练',
  favicon: '/logo.png',
  nodeModulesTransform: {
    type: UMI_ENV === "dev" ? 'none' : 'all',
  },
  routes: pageRoutes,
  targets: {
    android: 5,
    ios: 7,
    chrome: 58,
    ie: 9,
  },
  headScripts: [...headScripts],
  hash: true,
  //extraBabelPlugins: babelConfig,
  ignoreMomentLocale: true, // 忽略 moment 的 locale 文件，用于减少尺寸
  define: {
    FOO: 'bar',
    UMI_ENV, // 注册到全局
    RELEASE_VERSION: npm_package_version,
  },
  proxy: proxyConfig,
  theme: themeConfig,
  lessLoader: {
    modifyVars: {
      //hack: 'true; @import "~@/styles/index.less";',
    },
  },
  cssLoader: {
    localsConvention: 'camelCase',
  },
  analyze: {
    analyzerMode: 'server',
    analyzerPort: 8888,
    openAnalyzer: true,
    // generate stats file while ANALYZE_DUMP exist
    generateStatsFile: false,
    statsFilename: 'stats.json',
    logLevel: 'info',
    defaultSizes: 'parsed', // stat  // gzip
  },
  // 依赖提取
  chunks: ['vendors', 'umi'],
  chainWebpack(config) {
    config.merge({
      optimization: {
        //minimize: true,
        splitChunks: {
          chunks: 'all',
          cacheGroups: {
            vendors: {
              name: 'vendors',
              test({ resource }) {
                return /[\\/]node_modules[\\/]/.test(resource);
              },
              priority: 10,
            },
          },
        },
      },
    });
    if(UMI_ENV.includes('pro')){
      config.plugin('FileManagerPlugin').use(
        new FileManagerPlugin({
          events: {
            onEnd: {
              // delete: [
              //   './dist.zip'
              // ],
              archive: [ 
                {source: './dist', destination: './dist/dist.zip' }
              ]
            }
          }
        })
      )
    }

    // if (enableSentry && UMI_ENV !== 'mock' && !NODE_IS_DEV) {
    //   // 当为prod时候才进行sourcemap的上传，如果不判断，在项目运行的打包也会上传
    //   config.plugin('sentry').use(SentryPlugin, [
    //     {
    //       ignore: ['node_modules'],
    //       include: './dist', // 上传dist文件的js
    //       configFile: './sentryclirc', // 配置文件地址，这个一定要有，踩坑在这里，忘了写导致一直无法实现上传sourcemap
    //       release: npm_package_version, // 版本号，自己定义的变量，整个版本号在项目里面一定要对应
    //       deleteAfterCompile: true,
    //       urlPrefix: UMI_ENV === 'test' || UMI_ENV === 'pro' ? publicPathMap[UMI_ENV] : '~/', // cdn js的代码路径前缀
    //     },
    //   ]);
    // }
  },
});
